<?php
class Ranting extends CI_Controller{
    
		function  __construct() {
			parent::__construct();
			$this->load->model('Ranting_model');
			$this->load->helper(array('form', 'url'));
			$this->load->library('form_validation');
		 
		 }
	
		function index(){
	
			/*$unit = $this->Ranting_model->getUnit();
			foreach ($unit as $row){
				$data['intid_unit'][]	 	= $row->intid_unit;
				$data['strnama_unit'][] 	= $row->strnama_unit;
			}*/
			$data['show']=0;
			$data['unit']= 0;
			$this->load->view('admin_views/ranting/runit', $data);
		}
		
		function lookupUnit(){
		// process posted form data (the requested items like province)
        $keyword = $this->input->post('term');
        $data['response'] = 'false'; //Set default response
        $query = $this->Ranting_model->searchUnit($keyword); //Search DB
        if( ! empty($query) )
        {
            $data['response'] = 'true'; //Set response
            $data['message'] = array(); //Create array
            foreach( $query as $row )
            {
                $data['message'][] = array( 
                                        'id'=>$row->intid_unit,
                                        'value' => $row->strnama_unit,
                                        ''
                                     );  //Add a row to array
            }
        }
        if('IS_AJAX')
        {
            echo json_encode($data); //echo json string if ajax request
            
        }
        else
        {
            $this->load->view('admin_views/autocomplete/index',$data); //Load html view of search results
        }
	}

		function search(){
			$intid_unit = $this->input->post('intid_unit');
			$data['ranting'] = $this->Ranting_model->searchUnit($intid_unit);
			$unit = $this->Ranting_model->getUnit();
			foreach ($unit as $row){
				$data['intid_unit'][]	 	= $row->intid_unit;
				$data['strnama_unit'][] 	= $row->strnama_unit;
			}
			$data['show']=1;
			$data['unit']= $this->input->post('intid_unit');;
        	$this->load->view('admin_views/ranting/runit', $data);
		}
		
		function printXls()
		{
			$this->load->library('excel');
			$intid_unit = $this->uri->segment(4);
			to_excel($this->Ranting_model->searchUnit($intid_unit), 'Ranting');
			/*$this->excel->worksheet("Ranting Unit", 1, TRUE);
			
			$this->excel->smart_typing(true);
	
			$this->excel->cell('A1', 'No');
			$this->excel->cell('B1', 'Kode Dealer');
			$this->excel->cell('C1', 'Nama Dealer');
			$this->excel->cell('D1', 'Kode Upline');
			$this->excel->cell('E1', 'Nama Upline');
			
			$intid_unit = $this->uri->segment(4); 
			
			$data['data_laporan'] = $this->Ranting_model->searchUnit($intid_unit);
			
			$x = 2;
			$i=1;
			foreach($data['data_laporan']['grid'] as $row)
			{
				$this->excel->cell("A".$x."", $i);
				$this->excel->cell("B".$x."", $row['strkode_dealer']);
				$this->excel->cell("C".$x."", $row['strnama_dealer']);
				$this->excel->cell("D".$x."", $row['strkode_upline']);
				$this->excel->cell("E".$x."", $row['strnama_upline']);
				
				$i ++;
			}
			$this->excel->protectWorksheet(FALSE, null, 'Ranting Unit');
			$this->excel->output();	*/
	
		}
		
		function printPdf()
		{
			$intid_unit = $this->uri->segment(4);
			$data['ranting'] = $this->Ranting_model->searchUnit($intid_unit);
			$this->load->helper('to_pdf');
			$html = $this->load->view('admin_views/ranting/report',$data, true );
			pdf_create_lanscape($html, 'Ranting');
		}

	

}
?>
